#include "history.h"
#include "ui_history.h"

History::History(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::History)
{
    ui->setupUi(this);
    //setWindowFlags(Qt::FramelessWindowHint);
    setWindowFlags(Qt::WindowCloseButtonHint|Qt::WindowStaysOnTopHint);
    setupRealtimeDataDemo(ui->customPlot);
    ui->customPlot->replot();
    //    ui->customPlot->addGraph();



}

History::~History()
{
    delete ui;
}

void History::setupRealtimeDataDemo(QCustomPlot *customPlot)
{
    //#if QT_VERSION < QT_VERSION_CHECK(4, 7, 0)
    //QMessageBox::critical(this, "", "You're using Qt < 4.7, the realtime data demo needs functions that are available with Qt 4.7 to work properly");
    //#endif
    //demoName = "Real Time Data Demo";

    // include this section to fully disable antialiasing for higher performance:
    /*
      customPlot->setNotAntialiasedElements(QCP::aeAll);
      QFont font;
      font.setStyleStrategy(QFont::NoAntialias);
      customPlot->xAxis->setTickLabelFont(font);
      customPlot->yAxis->setTickLabelFont(font);
      customPlot->legend->setFont(font);
      */
    customPlot->addGraph(); // blue line
    customPlot->graph(0)->setPen(QPen(Qt::blue));
    customPlot->graph(0)->setName("temp");
    //customPlot->graph(0)->setBrush(QBrush(QColor(240, 255, 200)));
    //customPlot->graph(0)->setAntialiasedFill(false);
    customPlot->addGraph(); // red line
    customPlot->graph(1)->setPen(QPen(Qt::red));
    customPlot->graph(1)->setName("hui");
    //customPlot->graph(0)->setChannelFillGraph(customPlot->graph(1));


    customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
    customPlot->xAxis->setDateTimeFormat("hh:mm:ss");
    customPlot->xAxis->setAutoTickStep(false);
    customPlot->xAxis->setTickStep(2);
    customPlot->axisRect()->setupFullAxesBox();

    // make left and bottom axes transfer their ranges to right and top axes:
    //connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
    //connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));

    // setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
    dataTimer.start(0); // Interval 0 means to refresh as fast as possible
    customPlot->legend->setVisible(true);
}


void History::dataUpSlot(QByteArray arry)
{
    QVector<double> x,y;
    QVector<double> n,m;
    QString sjson(arry);

    qDebug()<< sjson;
    QJsonParseError jsonError;
    QJsonDocument doucment = QJsonDocument::fromJson(sjson.toLatin1(), &jsonError);
    if (!doucment.isNull() && jsonError.error == QJsonParseError::NoError)
    {
        if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError))
        {
            if (doucment.isObject())
            {
                QJsonObject object = doucment.object();
                if (object.contains("type"))
                {  // 包含指定的 key
                    QJsonValue value = object.value("type");
                    if (value.isDouble())
                    {
                        type=value.toVariant().toInt();
                        qDebug() << "type : " << type;
                    }
                }
                if(type ==4)
                {
                    if (object.contains("result"))
                    {
                        QJsonValue value = object.value("result");
                        if (value.isDouble())
                        {
                            result = value.toVariant().toInt();
                            qDebug() << "result : " << result;
                        }
                    }

                    if(result==0)
                    {
                        if(object.contains("data"))
                        {
                            QJsonValue value = object.value("data");
                            if(value.isArray())
                            {
                                QJsonArray arr=value.toArray();
                                int nsize=arr.size();
                                for(int i=0;i<nsize;i++)
                                {

                                    QJsonValue value=arr.at(i);
                                    if (value.isObject())
                                    {
                                        QJsonObject obj=value.toObject();
                                        if(obj.contains("time"))
                                        {
                                            QJsonValue value = obj.value("time");
                                            QString text=value.toString();
                                            qDebug()<<text;
                                            x.push_back(text.toDouble());
                                            n.push_back(text.toDouble());


                                        }
                                        if(obj.contains("temp"))
                                        {
                                            QJsonValue value = obj.value("temp");
                                            //QString text=value.toDouble();
                                            QString text=value.toString();
                                            y.push_back(text.toDouble());



                                        }
                                        if(obj.contains("humid"))
                                        {
                                            QJsonValue value = obj.value("humid");
                                            //QString text=value.toDouble();
                                            QString text=value.toString();
                                            m.push_back(text.toDouble());


                                        }
                                    }
                                }
                            }
                        }

                    }
                    ui->customPlot->graph(0)->setData(x,y);
                    ui->customPlot->graph(1)->setData(n,m);
                    ui->customPlot->xAxis->setRange(x.first(),x.last());
                    ui->customPlot->yAxis->setRange(0,100);
                    ui->customPlot->xAxis->setTickLabelRotation(35);
                    ui->customPlot->replot();
                }
            }
        }
    }
    //折线

}













